package com.purengage.ipilih;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.IntentAction;
import com.purengage.ipilih.lib.DBClass;
import com.purengage.ipilih.lib.Surveys;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class Survey2 extends TabActivity {
	String id;
	private TabHost mTabHost;

	private void setupTabHost() {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_survey2);
		
		Bundle extras = getIntent().getExtras();
		id = extras.getString("id");
		
		DBClass db = new DBClass(this);
		Surveys s = db.getSurveyById(Integer.parseInt(id));

		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle(s.getTitle().toString());		
		
		actionBar.setHomeAction(new IntentAction(this, Survey2
				.createIntent(this), R.drawable.ic_action_back));
		
		setupTabHost();		

		setupTab(new TextView(this), "Info", "Info.class", R.drawable.info_w);
		setupTab(new TextView(this), "Responden", "Responden.class", R.drawable.responden_w);
		setupTab(new TextView(this), "Kirim", "KirimResponden.class", R.drawable.truck_w);
		
		
	}
 
	private void setupTab(final View view, final String tag,
			final String className, final int drawable) {
		View tabview = createTabView(mTabHost.getContext(), tag, drawable);

		Intent intent;
		intent = new Intent().setClass(this, Info.class);
		if (className.equals("Info.class")) {
			intent = new Intent().setClass(this, Info.class);
			intent.putExtra("id", id);
		}
		if (className.equals("Responden.class")) {
			intent = new Intent().setClass(this, Responden.class);
			intent.putExtra("id", id);
		}
		if (className.equals("KirimResponden.class")) {
			intent = new Intent().setClass(this, KirimResponden.class);
			intent.putExtra("id", id);
		}

		TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
				.setContent(intent);
		
		mTabHost.addTab(setContent);
	}
	
	private static View createTabView(final Context context, final String text, final int drawable) {
		View view = LayoutInflater.from(context)
				.inflate(R.layout.tabs_bg, null);
		
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		ImageView iv = (ImageView) view.findViewById(R.id.imgTab);
		
		tv.setText(text);
		iv.setImageResource(drawable);
		return view;
	}
	
	public static Intent createIntent(Context context) {
		Intent i = new Intent(context, Main.class);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		i.putExtra("index", 1);
		return i;
	}
}
